<template>
  <el-form ref="form" class="login_container" :model="login" status-icon :rules="rules" label-width="70px">
    <!-- h3要放在里面:只能有一个根,且title也是表单的一部分 -->
    <h3 class="login_title">用户登录</h3>
    <!-- prop对应rules里的键 -->
    <el-form-item label="用户名" prop="userAcct">
      <el-input v-model="login.userAcct" autocomplete="off"></el-input>
    </el-form-item>

    <el-form-item label="密码" prop="password">
      <el-input type="password" v-model="login.password" autocomplete="off"></el-input>
    </el-form-item>

    <el-form-item>
      <el-button @click="submit" type="primary" style="margin-left:30px;margin-top:10px">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {login,getMenu} from '../api/index.js'
import {setToken} from "@/utils/cookies.js";
import {ElMessage} from "element-plus";

export default {
  data() {
    return {
      // 登陆数据
      login: {
        userAcct: '',
        password: ''
      },
      // 校验规则
      rules: {
        userAcct: [{required: 'true', message: '请输入用户名', trigger: 'blur'}],
        password: [{required: 'true', message: '请输入用户名', trigger: 'blur'}]
      }
    }
  },
  methods: {
    submit() {
      // 表单的校验
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 传入表单数据
          login(this.login).then((data) => {
            if(data.data.code === 200) {
              const token = data.data.token;
              setToken(token)
              getMenu().then((data) => {
                if (data.data.code === 200) {
                  console.log(12313123132131)
                  // 设置菜单
                  this.$store.commit('setMenu', data.data.data)
                  // 动态添加路由
                  // this.$store.commit('addMenu', this.$router)
                  console.log(data.data.data);
                  // 跳转到首页
                  this.$router.push('/home')
                }else {
                  this.$message.error(data.data.msg);
                }
              })
            }else {
              this.$message.error(data.data.msg?"执行异常":data.data.msg);
            }
          }).catch(()=>{
            ElMessage.error("执行异常")
          })
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  width: 350px;
  border: 1px solid #eaeaea;

  // 居中
  margin: 180px auto;

  padding: 35px 35px 15px 35px;

  // 让padding在width里面
  box-sizing: border-box;

  border-radius: 15px;
  background-color: #fff;
  box-shadow: 0 0 25px #cac6c6;

  .login_title {
    color: #505458;
    // 左右居中
    text-align: center;
    margin-bottom: 40px;
  }

  .el-input {
    width: 198px;
  }
}
</style>